// pages/sales/custom/manage_group.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");

//获取应用实例
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    groupList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    var ctx = this;
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      ctx.getGroups();
    });
  },
  ///applet/mobile/visitcard/relationgroup/findList
  getGroups(){
    var ctx = this;
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/relationgroup/findList",
      data: {
        cusmallToken: cusmallToken,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && 0 === data.ret) {
          let list = data.model.list;
          ctx.setData({
            groupList: data.model.list
          });
        } else {
          wx.showToast({
            title: data.msg,
            icon: "none"
          })
        }
      },
      fail() {

      },
      complete() {
        ctx.setData({ isLoading: false });
      }
    });
  },
  editGroup(e){
    let id = e.currentTarget.dataset.id;
    let idx = e.currentTarget.dataset.idx;

    wx.navigateTo({
      url: './add_group?gId=' + id,
    });

  },
  delGroupTips(e){
    let id = e.currentTarget.dataset.id;
    let idx = e.currentTarget.dataset.idx;
    var ctx = this;
    wx.showModal({
      title: '提示',
      content: '是否删除分组',
      success(res){
        if(res.confirm){
          ctx.delGroup(id, idx);
        }
      }
    })
  },
  delGroup(id, idx){
    var ctx = this;
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/relationgroup/del",
      data: {
        cusmallToken: cusmallToken,
        id: id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && 0 === data.ret) {
          var groupList = ctx.data.groupList;
          groupList.splice(idx,1);
          ctx.setData({
            groupList: groupList
          });
          wx.showToast({
            title: "删除成功",
            icon: "none"
          });
        } else {
          wx.showToast({
            title: data.msg,
            icon: "none"
          });
        }
      },
      fail() {

      },
      complete() {
        ctx.setData({ isLoading: false });
      }
    });
  },
  handleRefreshData(){
    this.getGroups();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})